﻿using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Interface.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CNKI.TPI.Web.Interface.Areas.Resource.Controllers
{
    public class GetSmartwordController : BaseApiController
    {
        // GET api/search/5
        /// <summary>
        /// 单库查询
        /// </summary>
        /// <param name="dbID">数据库id</param>
        /// <param name="dbCode">数据库编码</param>
        /// <param name="searchQuery">查询条件</param>
        /// <param name="fieldList">返回字段列表</param>
        /// <param name="catType">导航分类</param>
        /// <param name="catID">分类编码（逗号分隔）</param>
        /// <param name="order">排序</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <returns></returns>
        public object Get(string tag, string key, string length)
        {
            OutputEntity result = new OutputEntity();
            try
            {
                string content = BaseHttpClient.CallInterfaceGet(ConfigHelper.GetSmartwordAddress(), "tag=" + tag + "&param=" + key);
                if (content != null && content.Length > 0)
                {
                    content = content.Substring(1, content.Length - 2);
                    if (content.Contains(";"))
                    {
                        int count = int.Parse(length);
                        string[] wordList = content.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        if (wordList.Count() > count)
                        {
                            content = wordList[0];
                            for(int i = 1; i <count; i++)
                            {
                                content = content + ";" + wordList[i];
                            }
                        }
                    }
                }
                result.Status = Status.Success;
                result.data = content;
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
                result.Status = Status.Error;
                result.message = ex.Message;
            }
            return result;
        }
    }
}
